import sys
from pygame.locals import *
from random import *

from constants.ColorConstants import *
from constants.SystemConstants import *
from entity import bullets, hero_plane, enemies
import TextBox
# import database
import supply
import math

from page.ChangePage import ChangePage
from source import *
from utils.EntryHelp import EntryHelp
from utils.SoundControlUtils import SoundControlUtils



#主要游戏功能
def start(me_plane_type):

    #bgm设置
    SoundControlUtils.close_all_sound()
    SoundControlUtils.sound_bgm_putong.play(-1)
    SoundControlUtils.attack_sound.set_volume(0)


    boss_bg_size = boss_width, boss_height = 1920, 1080
    boss_screen = pygame.display.set_mode((1920, 1080), FULLSCREEN, 32)
    ty_bg_size = width, height = 600, 1000  # 背景像素
    ty_screen = pygame.display.set_mode(ty_bg_size)
    screen = ty_screen
    bg_size = ty_bg_size

    button_pause1_rect.left, button_pause1_rect.top = bg_size[0] - 65, 25
    background1 = bg_choic
    volume_bg_pos = 220
    volume_effect_pos = 220
    button_volume1_rect.left, button_volume1_rect.top = volume_bg_pos, 339
    button_volume2_rect.left, button_volume2_rect.top = volume_effect_pos, 439

    score = 0
    level = 1

    time = 0
    pause = False
    turn_plane_piture = False
    is_boss_cam = False
    boss_turn = True
    hero_is_skill = False
    hero2_skill = False
    energy_remain = 1
    screen_jump = 1
    skill2_now_tar = 0
    boss_skill2_index = 0
    is_warning = False
    warning_alpha = 0
    warning_alpha_dict = 1
    skill_last_time = 7
    is_max_att = False
    att_pro_limit = 600
    now_att_pro = 1
    now_att_pro_value = 0
    # 初始化背景

    INVICIBLE_TIME = USEREVENT
    SUPPLY_TIME = USEREVENT + 1
    BOSS_CAMPAIGN = USEREVENT + 2
    BOSS_CAMPAIGN_END = USEREVENT + 3
    HERO_SKILL_END = USEREVENT + 5

    boss_cam_end = pygame.event.Event(BOSS_CAMPAIGN_END)
    boss = []
    boss.append(enemies.boss(bg_size=boss_bg_size, level=1))
    # 根据选择确定战机
    if me_plane_type == 0:
        me = hero_plane.start1_plane(bg_size=bg_size)
    if me_plane_type == 1:
        me = hero_plane.start2_plane(bg_size=bg_size)
    if me_plane_type == 2:
        me = hero_plane.start3_plane(bg_size=bg_size)

    # 实例化敌机和boss
    # low_enemy = pygame.sprite.Group()
    low_enemy = []
    EntryHelp.add_small_enemies(low_enemy, 10)
    EntryHelp.add_mid_enemies(low_enemy, 5)
    EntryHelp.add_large_enemies(low_enemy, 3)
    enemy = low_enemy

    # 每30秒发放一个补给包
    blood_supply = supply.Blood_Supply(bg_size)
    power_supply = supply.Power_Supply(bg_size)
    upgrade_supply = supply.Upgrade_Supply(bg_size)
    pygame.time.set_timer(SUPPLY_TIME, 6 * 1000)

    # 实例化子弹
    bullet = []
    bullet_index = 0;
    BULLET_NUM = me.att_dis
    for i in range(BULLET_NUM):
        if me_plane_type == 0:
            bullet.append(bullets.Bullet1())
        elif me_plane_type == 1:
            bullet.append(bullets.Bullet2())
        else:
            bullet.append(bullets.Bullet3())
    boss_bullet = []
    boss_bullet_index = 0
    BOSS_BULLET_NUM = boss[0].bullet_num
    for i in range(BOSS_BULLET_NUM):
        boss_bullet.append(bullets.Boss_bullet((0, 0)))
    clock = pygame.time.Clock()

    boss_balls = []
    for i in range(boss[0].ball_num):
        boss_balls.append(bullets.Boss_ball())
    skill_bullet = []
    if me_plane_type == 1:
        for i in range(me.skill_num):
            skill_bullet.append(bullets.Skill_Bullet((1, 0)))
    # 操作开始
    n = 0
    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                return EXIT_PAGE
            if event.type == INVICIBLE_TIME:
                me.invincible = False
                pygame.time.set_timer(INVICIBLE_TIME, 0)
            if event.type == MOUSEBUTTONDOWN:
                pos = event.pos
                # 恢复暂停
                if resume_but_rect.left < pos[0] < resume_but_rect.right and \
                        resume_but_rect.top < pos[1] < resume_but_rect.bottom:
                    pause = False
                    SoundControlUtils.sound_button.play()
                    if is_warning:
                        pygame.time.set_timer(BOSS_CAMPAIGN, 3000)
                # 退回菜单
                elif button_back_rect.left < pos[0] < button_back_rect.right and \
                        button_back_rect.top < pos[1] < button_back_rect.bottom:
                    SoundControlUtils.sound_button.play()
                    os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (610, 40)
                    screen2 = pygame.display.set_mode((600, 1000))

                    #关闭全部声音
                    SoundControlUtils.close_all_sound()
                    StaticConstans.SCORE =score
                    return SCORE_PAGE
            if event.type == MOUSEMOTION and pause:
                pos = event.pos
                # 音量键滑动控制
                if button_volume1_rect.left < pos[0] < button_volume1_rect.right and \
                        button_volume1_rect.top - 30 < pos[1] < button_volume1_rect.bottom + 30 and \
                        line_left + 19 < pos[0] < line_right - 20:

                    button_volume1_rect.left = pos[0] - 20
                    #计算音量大小
                    H_t = (button_volume1_rect.left - line_left) / (line_right - line_left)
                    # 音量
                    SoundControlUtils.change_bgm_volume(H_t)

                # 音效滑块控制
                elif button_volume2_rect.left < pos[0] < button_volume2_rect.right and \
                        button_volume2_rect.top - 30 < pos[1] < button_volume2_rect.bottom + 30 and \
                        line_left + 19 < pos[0] < line_right - 20:
                    button_volume2_rect.left = pos[0] - 20
                    # 计算音量大小
                    H_t = (button_volume2_rect.left - line_left) / (line_right - line_left)
                    # 音效
                    SoundControlUtils.change_soundEffect_volume(H_t)
                    SoundControlUtils.sound_button.play()
            if event.type == MOUSEBUTTONDOWN:
                pos = event.pos
                # print(pause)
                # 暂停
                if button_pause1_rect.left < pos[0] < button_pause1_rect.right and \
                        button_pause1_rect.top < pos[1] < button_pause1_rect.bottom:
                    SoundControlUtils.sound_button.play()

                    pause = True
            if event.type == SUPPLY_TIME:
                # supply_sound.play()
                choice = randint(1, 3)
                if choice == 1:
                    power_supply.reset()
                elif choice == 2:
                    blood_supply.reset()
                else:
                    upgrade_supply.reset()
            if event.type == KEYDOWN:
                if event.key == K_SPACE:
                    if me.power > 0 and not hero_is_skill:
                        me.power -= 1
                        hero_is_skill = True
                        if me_plane_type == 0:
                            me.att_speed = 8
                            me.invincible = True    #飞机无敌两秒
                            pygame.time.set_timer(INVICIBLE_TIME, 2 * 1000)
                        elif me_plane_type == 1:
                            SoundControlUtils.sound_hero2skill.play()
                            hero2_skill = True
                            bullet[0].power *= 2
                        else:
                            me.att_speed = 40
                            for i in range(me.att_dis):
                                bullet[i].speed = (math.cos(i / 10 * 2 * math.pi) * 10,
                                                   math.sin(i / 10 * 2 * math.pi) * 10)
                        pygame.time.set_timer(HERO_SKILL_END, skill_last_time * 1000)

            if event.type == HERO_SKILL_END:

                pygame.time.set_timer(HERO_SKILL_END, 0)

                hero_is_skill = False
                if me_plane_type == 0:
                    me.att_speed = 10

                elif me_plane_type == 1:
                    bullet[0].power /= 2
                    for b in skill_bullet:
                        b.active = False
                elif me_plane_type == 2:
                    me.att_speed = 60
                    for b in bullet:
                        b.speed = [6, 0]

            if event.type == BOSS_CAMPAIGN:
                power_supply.width, power_supply.height = 1920, 1080
                blood_supply.width, power_supply.height = 1920, 1080
                upgrade_supply.width, power_supply.height = 1920, 1080
                #关闭全部声音
                SoundControlUtils.close_all_sound()
                #
                SoundControlUtils.sound_bgm_boss.play(-1)
                is_warning = False
                pygame.time.set_timer(BOSS_CAMPAIGN, 0)
                for b in skill_bullet:
                    b.active = False
                screen = pygame.display.set_mode((1920, 1080), FULLSCREEN, 32)
                bg_size = width, height = 1920, 1080  # 背景像素
                background = boss_background
                button_pause1_rect.left, button_pause1_rect.top = bg_size[0] - 65, 25
                is_boss_cam = True
                button_volume1_rect.left, button_volume1_rect.top = 850, 339
                button_volume2_rect.left, button_volume2_rect.top = 850, 438
                me.bg_width, me.bg_height = bg_size
                me.rect.left, me.rect.top = \
                    (me.bg_width - me.rect.width) / 2, \
                    me.bg_height - me.rect.height - 30
                enemy = boss
                boss[0] = enemies.boss(bg_size=boss_bg_size, level=level)

            if event.type == BOSS_CAMPAIGN_END:
                SoundControlUtils.sound_bgm_boss.stop()
                power_supply.width, power_supply.height = 600, 1000
                blood_supply.width, power_supply.height = 600, 1000
                upgrade_supply.width, power_supply.height = 600, 1000
                if level < 3:
                    SoundControlUtils.sound_bgm_putong.play(-1)
                elif level < 5:
                    SoundControlUtils.sound_bgm_level2.play(-1)
                else:
                    SoundControlUtils.sound_lastone.play(-1)

                os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (610, 40)
                bg_size = width, height = 600, 1000  # 背景像素
                button_pause1_rect.left, button_pause1_rect.top = bg_size[0] - 65, 25
                button_volume1_rect.left, button_volume1_rect.top = 220, 338
                button_volume2_rect.left, button_volume2_rect.top = 220, 438
                screen = pygame.display.set_mode(ty_bg_size)

                if level == 2:
                    background1 = bg_fight2
                elif level == 3:
                    background1 = bg_fight3
                    SoundControlUtils.close_all_sound()
                    SoundControlUtils.sound_bgm_level2.play(-1)
                elif level == 4:
                    background1 = bg_fight4
                else:
                    background1 = bg_fight1
                    SoundControlUtils.close_all_sound()
                    SoundControlUtils.sound_lastone.play(-1)
                is_boss_cam = False
                me.bg_width, me.bg_height = bg_size
                me.rect.left, me.rect.top = \
                    (me.bg_width - me.rect.width) / 2, \
                    me.bg_height - me.rect.height - 30
                enemy = low_enemy

        # 根据用户得分增加难度
        if level == 1 and score > 10000:
            level = 2
            EntryHelp.add_small_enemies(enemy, 3)
            EntryHelp.add_mid_enemies(enemy, 2)
            EntryHelp.add_large_enemies(enemy, 1)
            EntryHelp.add_level(enemy)
            pygame.time.set_timer(BOSS_CAMPAIGN, 3000)
            is_warning = True


        elif level == 2 and score > 25000:
            level = 3
            EntryHelp.add_small_enemies(enemy, 3)
            EntryHelp.add_mid_enemies(enemy, 2)
            EntryHelp.add_large_enemies(enemy, 1)
            EntryHelp.add_level(enemy)
            pygame.time.set_timer(BOSS_CAMPAIGN, 3000)
            is_warning = True

        elif level == 3 and score > 45000:
            level = 4
            EntryHelp.add_small_enemies(enemy, 3)
            EntryHelp.add_mid_enemies(enemy, 2)
            EntryHelp.add_large_enemies(enemy, 1)
            EntryHelp.add_level(enemy)
            pygame.time.set_timer(BOSS_CAMPAIGN, 3000)
            is_warning = True

        elif level == 4 and score > 70000:
            level = 5
            EntryHelp.add_small_enemies(enemy, 5)
            EntryHelp.add_mid_enemies(enemy, 4)
            EntryHelp.add_large_enemies(enemy, 3)
            EntryHelp.add_level(enemy)
            pygame.time.set_timer(BOSS_CAMPAIGN, 3000)
            is_warning = True

        # 进行游戏的操作
        if me.blood > 0 and not pause:
            # 键盘操作
            key_pressed = pygame.key.get_pressed()
            if key_pressed[K_UP]:
                me.MoveUp()
            if key_pressed[K_DOWN]:
                me.MoveDowm()
            if key_pressed[K_LEFT]:
                me.MoveLeft()
            if key_pressed[K_RIGHT]:
                me.MoveRight()
            if key_pressed[K_ESCAPE]:
                pause = True

            # 主角2技能
            if hero2_skill == True:
                for b in skill_bullet:
                    b.has_target = True
                    # b.reset((randint(0,bg_size[0]),randint(bg_size[1],bg_size[1] + 200)))
                    b.reset((randint(0, bg_size[0]), randint(bg_size[1], bg_size[1] + 200)), 1)
                    #now_tar 导弹目标
                    now_tar = skill2_now_tar
                    skill2_now_tar = (skill2_now_tar + 1) % len(enemy)
                    while enemy[skill2_now_tar].rect.bottom < 0:
                        skill2_now_tar = (skill2_now_tar + 1) % len(enemy)
                        if skill2_now_tar == now_tar:
                            b.has_target = False
                            break
                    b.target = skill2_now_tar
                # for b in skill_bullet :
                #     b.reset((randint(0, bg_size[0]), randint(bg_size[1],bg_size[1] + 200)))
                #     b.target = 0
            hero2_skill = False

            # 画布背景移动偏移量改变
            if n == 2000:
                n = 0
            n += 1


            if is_boss_cam and boss[0].is_skill2:
                if not (time % 1):
                    screen_jump = (screen_jump + 1) % 8
                screen.blit(background, (0 + 10 * SCREEN_OFFECTX[screen_jump], 10 + 20 * SCREEN_OFFECTY[screen_jump]))

            else:
                if is_boss_cam:
                    screen.blit(background, (0, 0))
                else:
                    screen.blit(background1, (0, -2000 + n))

            # 绘制2号机技能
            if me_plane_type == 1:
                for b in skill_bullet:
                    if b.active:
                        #每过10帧子弹速度加1
                        if not (time % 10):
                            b.speed1 += 1

                        #
                        if enemy[b.target].rect.bottom < 0:
                            now_tar = skill2_now_tar
                            b.has_target = True
                            skill2_now_tar = (skill2_now_tar + 1) % len(enemy)
                            while enemy[skill2_now_tar].rect.bottom < 0:
                                skill2_now_tar = (skill2_now_tar + 1) % len(enemy)
                                if skill2_now_tar == now_tar:
                                    b.has_target = False
                                    break
                            b.target = skill2_now_tar

                        #导弹向敌人坐标移动
                        b.Move(enemy[b.target])

                        #朝敌人方向转动 ，设置图片
                        new_bullet = pygame.transform.rotate(b.image1, 270 + b.dict * 180 / math.pi)
                        new_rect = new_bullet.get_rect(center=b.rect.center)
                        screen.blit(new_bullet, new_rect)

            # 绘制能量炸弹补给
            if power_supply.active:
                power_supply.move()
                screen.blit(power_supply.image, power_supply.rect)
                if pygame.sprite.collide_mask(me, power_supply):
                    SoundControlUtils.sound_bgm_supply.play()
                    if me.power < 3:
                        me.power += 1
                    power_supply.active = False

            # 绘制血量补给
            if blood_supply.active:
                blood_supply.move()
                screen.blit(blood_supply.image, blood_supply.rect)
                if pygame.sprite.collide_mask(me, blood_supply):
                    SoundControlUtils.sound_bgm_supply.play()
                    if me.blood > me.max_blood / 2:
                        me.blood += 2
                    else:
                        me.blood += (me.max_blood - me.blood) / 3
                    blood_supply.active = False
                    if me.blood > me.max_blood:
                        me.blood = me.max_blood
            # 升级包补给
            if upgrade_supply.active:
                upgrade_supply.move()
                screen.blit(upgrade_supply.image, upgrade_supply.rect)
                if pygame.sprite.collide_mask(me, upgrade_supply):
                    SoundControlUtils.sound_bgm_supply.play()
                    me.max_blood += 2
                    me.blood += 1
                    if me.speed < me.max_speed:
                        me.speed += 1
                    upgrade_supply.active = False

            # 发射子弹
            if not (time % me.att_speed):
                if me_plane_type == 0:
                    SoundControlUtils.sound_hero1bullet.play()
                    if hero_is_skill == True:
                        bullet[bullet_index].reset((me.rect.centerx - 33, me.rect.centery))
                        bullet_index = (bullet_index + 1) % BULLET_NUM
                        bullet[bullet_index].reset((me.rect.centerx + 30, me.rect.centery))
                        bullet_index = (bullet_index + 1) % BULLET_NUM
                        bullet[bullet_index].reset(me.rect.midtop)
                        bullet_index = (bullet_index + 1) % BULLET_NUM
                if me_plane_type == 1:
                    SoundControlUtils.sound_hero2bullet.play()
                if me_plane_type == 2:
                    SoundControlUtils.sound_hero3bullet.play()
                    if hero_is_skill == True:
                        for i in range(0, 10):
                            bullet[bullet_index].reset(me.rect.midtop)
                            bullet_index = (bullet_index + 1) % BULLET_NUM
                bullet[bullet_index].reset(me.rect.midtop)
                bullet_index = (bullet_index + 1) % BULLET_NUM

            # boss放技能
            if not (time % boss[0].skill_intrival):
                boss[0].is_skill = True
                # skill_type = (skill_type + 1) % 2 + 1
            # boss技能一发射弹幕
            if is_boss_cam and boss[0].is_skill and boss[0].skill_index == 1:
                SoundControlUtils.attack_sound.play()
                dose = 2 * math.pi / BOSS_BULLET_NUM
                offset = float(randint(0, 10)) / 10
                for i in range(BOSS_BULLET_NUM):
                    boss_bullet[boss_bullet_index].reset(boss[0].rect.midtop, (
                        (5 + 2 * level) * math.sin((i + offset) * dose),
                        (5 + 2 * level) * math.cos((i + offset) * dose)))
                    boss_bullet_index = (boss_bullet_index + 1) % BOSS_BULLET_NUM

            # boss二技能掉落火球
            if is_boss_cam and boss[0].in_skill2 and not boss[0].is_skill2 and not (
                    time % int(120 / (1 + 0.7 * level))):
                boss_balls[boss_skill2_index].reset((randint(0, bg_size[0]), -200))
                boss_skill2_index = (boss_skill2_index + 1) % boss[0].ball_num

            if is_boss_cam and not boss[0].in_skill2 and energy_remain < boss[0].low_limit:
                SoundControlUtils.sound_boss3.play()
                boss[0].is_skill2 = True
                boss[0].in_skill2 = True
                for each in boss_balls:
                    each.speed[0] -= level

            # 绘制子弹
            for b in bullet:
                if b.active:
                    if me_plane_type == 1:
                        b.Move(me.rect.midtop)
                    else:
                        b.Move(bg_size)
                    if not (time % 60):
                        b.turn_image()
                    screen.blit(b.show_image, b.rect)
            # 绘制boss子弹
            if is_boss_cam:
                for b in boss_bullet:
                    if b.active:
                        b.Move(boss_bg_size)
                        if boss_turn:
                            boss_screen.blit(b.image1, b.rect)
                        else:
                            boss_screen.blit(b.image2, b.rect)

            # 绘制boss火球
            if is_boss_cam and boss[0].in_skill2:
                for b in boss_balls:
                    if b.active:
                        b.Move(boss_bg_size)
                        new_ball = pygame.transform.rotate(b.image1, b.angle)
                        new_ball_rect = new_ball.get_rect(center=b.rect.center)
                        b.angle += 5
                        boss_screen.blit(new_ball, new_ball_rect)

            # 处理子弹
            for b in bullet:
                if b.active:
                    hited_enemy = pygame.sprite.spritecollide(b, enemy, False, pygame.sprite.collide_mask)
                    if hited_enemy:
                        if me_plane_type == 0:
                            b.active = False
                        for each in hited_enemy:
                            if each.rect.bottom > 0:
                                if me_plane_type == 0:
                                    now_att_pro_value += 2
                                else:
                                    now_att_pro_value +=0.5
                                if now_att_pro_value >= att_pro_limit and not is_max_att:
                                    SoundControlUtils.sound_upgrade.play()
                                    now_att_pro_value-=att_pro_limit
                                    now_att_pro += 0.1
                                    if now_att_pro == me.max_att_pro:
                                        is_max_att = False

                                each.is_hit = True
                                each.blood -= b.power * now_att_pro

            for b in skill_bullet:
                if b.active:
                    hited_enemy = pygame.sprite.spritecollide(b, enemy, False, pygame.sprite.collide_mask)
                    if hited_enemy:
                        for each in hited_enemy:
                            each.blood -= b.power
                            b.active = False

            # 主角飞机撞击检测
            if not (time % 5):
                enemy_col = \
                    pygame.sprite.spritecollide(me, enemy, False, pygame.sprite.collide_mask)
                if enemy_col:
                    if not me.invincible:
                        me.active = False
                        me.blood -= 2
                        me.invincible = True
                    for each in enemy_col:
                        each.blood -= 2
                        each.is_hit = True
            if is_boss_cam:
                boss_bullet_col = \
                    pygame.sprite.spritecollide(me, boss_bullet, False, pygame.sprite.collide_mask)
                if boss_bullet_col:
                    for each in boss_bullet_col:
                        if not me.invincible and each.active:
                            me.active = False
                            me.blood -= each.power
                            me.invincible = True
                            each.active = False
                if boss[0].in_skill2:
                    boss_ball_col = \
                        pygame.sprite.spritecollide(me, boss_balls, False, pygame.sprite.collide_mask)
                    if boss_ball_col:
                        me.active = False
                        me.blood -= boss_ball_col[0].power
                        me.invincible = True

            # 绘制主角战机
            if me.active:
                if me.invincible:
                    image_protect1_rect.left = me.rect.centerx - (image_protect1_rect.width) / 2
                    image_protect1_rect.top = me.rect.centery - (image_protect1_rect.height) / 2
                    screen.blit(image_protect1, image_protect1_rect)
                if turn_plane_piture:
                    screen.blit(me.fly_image1, me.rect)
                else:
                    screen.blit(me.fly_image2, me.rect)
            else:
                if me.die_index == 0:
                    SoundControlUtils.hero_die_sound.play()
                if not (time % 3):
                    screen.blit(me.die_images[me.die_index], me.rect)
                    me.die_index = (me.die_index + 1) % 4
                    if me.die_index == 0:
                        me.active = True
                        pygame.time.set_timer(INVICIBLE_TIME, 1 * 1000)

            # 绘制敌机
            for each in enemy:
                if each.blood > 0:
                    # if each.rect.top < 100 :
                    #     each.disappear_move()
                    # else:
                    # if  not is_boss_cam or not each.is_skill2:
                    #     each.move()
                    if not is_boss_cam:
                        if each.rect.bottom < 0:
                            if not is_warning and not is_boss_cam:
                                each.disappear_move()
                        else:
                            each.move()
                    elif not each.is_skill2:
                        each.move()
                    if each.is_hit:
                        each.is_hit = False
                        if not is_boss_cam or (not each.is_skill and not each.is_skill2):
                            screen.blit(each.image_hited, each.rect)
                    if is_boss_cam and each.is_skill2:
                        if each.skill_index == 0:
                            SoundControlUtils.small_die_sound.play()
                        if not (time % 15):
                            boss_screen.blit(each.skill2[each.skill_index], each.rect)
                            each.skill_index = (each.skill_index + 1) % len(each.skill2)
                            if each.skill_index == 0:
                                each.is_skill2 = False
                                time = 1
                                each.skill_index = 0
                        boss_screen.blit(each.skill2[each.skill_index], each.rect)
                    elif is_boss_cam and each.is_skill:
                        if each.skill_index == 0:
                            SoundControlUtils.small_die_sound.play()
                        if not (time % 30):
                            boss_screen.blit(each.skill1[each.skill_index], each.rect)
                            each.skill_index = (each.skill_index + 1) % len(each.skill1)
                            if each.skill_index == 0:
                                each.is_skill = False
                                each.skill_index = 0
                        boss_screen.blit(each.skill1[each.skill_index], each.rect)


                    elif boss_turn:
                        screen.blit(each.image1, each.rect)
                    else:
                        screen.blit(each.image2, each.rect)
                    energy_remain = each.blood / each.max_blood
                    if energy_remain > 0.2:
                        energy_color = COLOR_GREEN
                    else:
                        energy_color = COLOR_RED
                    if not is_boss_cam:
                        pygame.draw.line(screen, COLOR_BLACK,
                                         (each.rect.left, each.rect.top - 5),
                                         (each.rect.right, each.rect.top - 5),
                                         2)

                        pygame.draw.line(screen, energy_color,
                                         (each.rect.left, each.rect.top - 5),
                                         (each.rect.left + each.rect.width * energy_remain,
                                          each.rect.top - 5),
                                         2)
                    else:
                        pygame.draw.line(boss_screen, COLOR_BLACK,
                                         (420, 50),
                                         (1500, 50),
                                         20)
                        pygame.draw.line(boss_screen, energy_color,
                                         (420, 50),
                                         (420 + (1500 - 420) * energy_remain, 50),
                                         20)

                elif not is_boss_cam:
                    if each.die_index == 0:
                        SoundControlUtils.small_die_sound.play()
                    if not (time % 2):
                        screen.blit(each.die_image[each.die_index], each.rect)
                        each.die_index = (each.die_index + 1) % len(each.die_image)
                        if each.die_index == 0:
                            each.reset();
                            score += each.score
                            if is_boss_cam:
                                pygame.event.post(boss_cam_end)
                else:
                    print(each.die_index)
                    if each.die_index == 0:
                        # small_die_sound.play()
                        SoundControlUtils.sound_boss_boom.play(-1)
                    if not (time % 10):
                        each.die_index = (each.die_index + 1) % len(each.die_image)
                        if each.die_index == 0:
                            each.reset();
                            score += each.score
                            if is_boss_cam:
                                pygame.event.post(boss_cam_end)
                                SoundControlUtils.sound_boss_boom.stop()
                    screen.blit(each.die_image[each.die_index], each.rect)


            #攻击力提升

            att_text = att_font.render('当前攻击力加成 %d %%' % (now_att_pro * 100 - 100), True, COLOR_WHILE)
            screen.blit(att_text , (25,215))
            pygame.draw.line(screen, COLOR_BLACK,
                             (20,200),
                             (170, 200),
                             10)
            pygame.draw.line(screen, (255,255,128),
                             (20, 200),
                             (20 + 150 * min(now_att_pro_value/att_pro_limit , 1), 200),
                             10)


            # 血条：(15，90) (184,101)
            screen.blit(blood_slot, (0, -30))
            # pygame.draw.line(screen, COLOR_WHILE,
            #                  (15, 65),
            #                  (184,65),
            #                  12)
            blood_per = me.blood / me.max_blood
            pygame.draw.line(screen, (208, 47, 63),
                             (15, 65),
                             (15 + (184 - 15) * blood_per, 65),
                             12)
            score_text = score_font.render('Score : %d' % score, True, COLOR_WHILE)
            screen.blit(score_text, (10, 120))
            blood_text = blood_text_font.render("%d/%d" % (me.blood, me.max_blood), True, COLOR_BLACK)
            screen.blit(blood_text, ((15 + (184 - 15 - blood_text.get_rect().width) // 2), 55))

            # 能量条
            # pygame.draw.line(screen, COLOR_WHILE,
            #                  (15, 109),
            #                  (184, 109),
            #                  12)
            power_per = me.power / me.max_power
            pygame.draw.line(screen, (0, 0, 255),
                             (15, 79),
                             (15 + (184 - 15) * power_per, 79),
                             12)
            power_text = blood_text_font.render("%d/%d" % (me.power, me.max_power), True, COLOR_BLACK)
            screen.blit(power_text, ((15 + (184 - 15 - blood_text.get_rect().width) // 2), 69))

            # 打印警告界面
            if is_warning and not is_boss_cam:
                warning_alpha = warning_alpha_dict * 8 + warning_alpha
                if warning_alpha >= 256 or warning_alpha <= 0:
                    if warning_alpha >= 256:
                        SoundControlUtils.sound_warning.play()
                    warning_alpha_dict = - warning_alpha_dict
                warning_image.set_alpha(warning_alpha)
                warning_image_rect.left = (bg_size[0] - warning_image_rect.width) / 2
                warning_image_rect.top = 300
                screen.blit(warning_image, warning_image_rect)

        if pause:
            if is_warning:
                pygame.time.set_timer(BOSS_CAMPAIGN, 0)
            pausing_image_rect.left = (bg_size[0] - pausing_image_rect.width) // 2
            pausing_image_rect.top = 200
            button_back_rect.left = pausing_image_rect.left + 95
            button_back_rect.bottom = pausing_image_rect.bottom - 100
            v_bg = font_volume.render("音量", True, COLOR_BLACK)
            v_ef = font_volume.render("音效", True, COLOR_BLACK)
            v_set = font_volume.render("设置", True, COLOR_BLACK)
            line_left, line_right = pausing_image_rect.left + 150, pausing_image_rect.left + 400
            resume_but_rect.left, resume_but_rect.top = pausing_image_rect.right - resume_but_rect.width - 7, \
                                                        pausing_image_rect.bottom - resume_but_rect.width - 7

            screen.blit(pausing_image, pausing_image_rect)
            screen.blit(resume_but, resume_but_rect)
            screen.blit(button_back, button_back_rect)
            screen.blit(v_bg, (pausing_image_rect.left + 50, pausing_image_rect.top + 130))
            screen.blit(v_ef, (pausing_image_rect.left + 50, pausing_image_rect.top + 230))
            screen.blit(v_set, (pausing_image_rect.left + 190, pausing_image_rect.top + 50))
            # 划线
            pygame.draw.line(screen, COLOR_BLACK,
                             (line_left, pausing_image_rect.top + 155,),
                             (line_right, pausing_image_rect.top + 155),
                             6)
            pygame.draw.line(screen, COLOR_BLACK,
                             (line_left, pausing_image_rect.top + 255,),
                             (line_right, pausing_image_rect.top + 255),
                             6)
            pygame.draw.line(screen, COLOR_GREEN,
                             (line_left + 2, button_volume2_rect.top + 17,),
                             (button_volume2_rect.left, button_volume2_rect.top + 17),
                             3)
            pygame.draw.line(screen, COLOR_GREEN,
                             (line_left + 2, button_volume1_rect.top + 17,),
                             (button_volume1_rect.left, button_volume1_rect.top + 17),
                             3)

            screen.blit(button_volume1, button_volume1_rect)
            screen.blit(button_volume2, button_volume2_rect)
            pygame.display.flip()
        else:
            screen.blit(button_pause1, button_pause1_rect)
        if me.blood <= 0:
            if is_boss_cam:
                pygame.event.post(boss_cam_end)
            else:
                return score
        if time % 11 == 0:
            turn_plane_piture = not turn_plane_piture
        if time % 30 == 0:
            boss_turn = not boss_turn
        time = (time + 1) % 3000
        pygame.display.flip()
        clock.tick(60)
    # 登录注册之后的界面界面











if __name__ == '__main__':
    try:
        #登录注册界面一定要用英文输入法
        #数据库连接可能不成功

        #跳过登录注册进入主界面
        #main("asd")

        # print(math.cos(math.pi))
        page = REG_LOGIN_PAGE
        while True:
            #原始登录页面
            if page == REG_LOGIN_PAGE:
                ChangePage.paint_reg_login()
                page = ChangePage.reg_login_action()

            #登录页面
            elif page == LOGIN_PAGE:
                page = ChangePage.paint_login(True)

            #注册页面
            elif page == REGISTER_PAGE:
                page = ChangePage.paint_register_page(False)

            #主页面
            elif page == MAIN_PAGE:
                 page = ChangePage.paint_main_page()

            #排行榜页面
            elif page == RANK_PAGE:
                page = ChangePage.paint_rank()

            #操作说明页面
            elif page == OPERATING_INSTRUCTIONS_PAGE:
                page = ChangePage.paint_opreat()

            #选择战机界面
            elif page == CHOOSE_PLANE_PAGE:
                page = ChangePage.paint_choose_plane()

            #开始游戏界面
            elif page == START_PAGE:
                page = start(StaticConstans.PLANE_NUMBER)

            #分数展示页面
            elif page == SCORE_PAGE:
                page = ChangePage.end_paint(StaticConstans.USER_ID,StaticConstans.SCORE)
            elif page == EXIT_PAGE:
                pygame.quit()
                sys.exit()





    except SystemExit:
        pass
